import React, { useEffect, useState } from 'react';
import { Form, message } from 'antd';
import { AxCreateForm } from '@/components/AxForm/AxCreateForm';
import { AxLabel } from '@/components/AxForm/AxFormItem';
import { axhttp } from '@/shared/axhttp';
import { axurl } from '@/shared/axurl';
import { AxUpload } from '@/components/AxForm/AxFormItem/AxUpload';
import UploadInfo from '@/components/AxHeader/UploadInfo';

const { postFormX } = axhttp;
const url = axurl.charge.s.tuition.fee.collect;
const Import: React.FC<any> = props => {
    const { form, ...rest } = props;
    const [uploadVisible, setUploadVisible] = useState(false);
    const [fileList, setFileList] = useState<any>([]);

    useEffect(() => {
    }, []);

    const handleCreate = (fields: any) => {
        return postFormX(url.import, null, fields).then((res: any) => {
            if (res.data === true) {
                setUploadVisible(true);
                setFileList([]);
                form.resetFields();
            } else message.error('导入失败');
        });
    };

    const uploadProps = {
        visible: uploadVisible,
        handleVisible: setUploadVisible,
    };

    return (
        <>
            <AxCreateForm
                form={form}
                title='上传物资'
                handleCreate={handleCreate}
                {...rest}
                items={[
                    <AxUpload
                        form={form}
                        label="文件"
                        field="attachmentIds"
                        uploadUrl={url.uploadFile}
                        fileList={fileList}
                        required
                    />,
                    <AxLabel form={form} label="导入模板" field="template">
                        <a href={axurl.template.charge_tuition_fee_collect} target="_blank">
                            库存初期盘点导入模板.xlsx
                  </a>
                    </AxLabel>,
                ]}
            />
            {uploadVisible && <UploadInfo {...uploadProps} />}
        </>
    );
};

export default Form.create<any>()(Import);
